package com.mew.chess.core.game.shbz.game.common;

import com.mew.chess.core.game.shbz.game.ShbzGameManager;
import com.mew.chess.core.game.shbz.service.ShbzService;
import com.mew.common.util.GameUtil;
import com.mew.common.util.NumberUtil;
import com.zebra.common.concurrent.ZebraExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//大炮配置
@Service
@Slf4j
public class ArtilleryUtils {

    @Autowired
    private ShbzService shbzService;

    private static long artilleryBet1 = 50000;
    private static int[] artilleryType1 = {0,1,2,-1};
    private static int[] artilleryWeight1 = {500,0,0,500};


    private static long artilleryBet2 = 500000;
    private static int[] artilleryType2 = {1,2,3,-1};
    private static int[] artilleryWeight2 = {500,0,0,500};


    private static long artilleryBet3 = 5000000;
    private static int[] artilleryType3 = {2,3,4,-1};
    private static int[] artilleryWeight3 = {500,0,0,500};


    private static long artilleryBet4 = 50000000;
    private static int[] artilleryType4 = {2,3,4,-1};
    private static int[] artilleryWeight4 = {500,250,20,230};

    private static String artilleryGold0 = "600-1400";
    private static String artilleryGold1 = "6000-14000";
    private static String artilleryGold2 = "60000-140000";
    private static String artilleryGold3 = "600000-1400000";
    private static String artilleryGold4 = "6000000-14000000";

    public  int[] getArtillery(int roomType,long bet,FishUserBO fishUserBO){
        long artilleryBet = fishUserBO.getArtilleryBet() + bet;


        if (roomType==1 && artilleryBet>=artilleryBet1){
            return doArtillery(artilleryType1,artilleryWeight1,fishUserBO);
        }

        if (roomType==2 && artilleryBet>=artilleryBet2){
            return doArtillery(artilleryType2,artilleryWeight2,fishUserBO);
        }

        if (roomType==3 && artilleryBet>=artilleryBet3){
            return doArtillery(artilleryType3,artilleryWeight3,fishUserBO);
        }

        if (roomType==4 && artilleryBet>=artilleryBet4){
            return doArtillery(artilleryType4,artilleryWeight4,fishUserBO);
        }

        fishUserBO.setArtilleryBet(artilleryBet);

        return null;

    }

    private  int[] doArtillery(int[] type ,int[] weight,FishUserBO fishUserBO){
        int a = type[GameUtil.weights(weight)];
        long artilleryBet = fishUserBO.getArtilleryBet();
        fishUserBO.setArtilleryBet(0);
        if (a>=0){
            fishUserBO.getArtillerys()[a]+=1;
            log.info("shbz,arti,r,{},t:{},uid,{},消耗金币,{},获取炸弹類型,{},当前炸弹,{}",fishUserBO.getRoomId(),fishUserBO.getTableId(),fishUserBO.getUserId(),artilleryBet,a,fishUserBO.getArtillerys());
            return fishUserBO.getArtillerys();
        }
        log.info("shbz,arti,r,{},t:{},uid,{},消耗金币,{},获取炸弹類型,{},当前炸弹,{}",fishUserBO.getRoomId(),fishUserBO.getTableId(),fishUserBO.getUserId(),artilleryBet,a,fishUserBO.getArtillerys());
        return null;
    }

    public  long dorp(int[] arti, FishUserBO fishUserBO, ShbzGameManager manager){

        ZebraExecutor executor = manager.getDisruptorByUserId(fishUserBO.getUserId());
        long gold = 0;

        int[] artillerys = fishUserBO.getArtillerys();
        if (arti[0]>0 && artillerys[0]>=arti[0]){
            gold += NumberUtil.randomIntByStr(artilleryGold0, "-")*arti[0];
            artillerys[0] -= arti[0];
        }
        if (arti[1]>0 && artillerys[1]>=arti[1]){
            gold += NumberUtil.randomIntByStr(artilleryGold1, "-")*arti[1];
            artillerys[1] -= arti[1];
        }
        if (arti[2]>0 && artillerys[2]>=arti[2]){
            gold += NumberUtil.randomIntByStr(artilleryGold2, "-")*arti[2];
            artillerys[2] -= arti[2];
        }
        if (arti[3]>0 && artillerys[3]>=arti[3]){
            gold += NumberUtil.randomIntByStr(artilleryGold3, "-")*arti[3];
            artillerys[3] -= arti[3];
        }
        if (arti[4]>0 && artillerys[4]>=arti[4]){
            gold += NumberUtil.randomIntByStr(artilleryGold4, "-")*arti[4];
            artillerys[4] -= arti[4];
        }

        shbzService.upWinFree(fishUserBO.getUserId(),fishUserBO.getRoomId(),fishUserBO.getTableId());
        gold = gold/100 * 100;

        long finalGold = gold;
        executor.execute(()->{
            fishUserBO.setPayment(fishUserBO.getPayment()+ finalGold);
        });
        fishUserBO.updateGoldNumber(gold);


        log.info("shbz,arti,r,{},t,{},uid,{},获取金币,{},丢炸弹,{},当前炸弹,{}",fishUserBO.getRoomId(),fishUserBO.getTableId(),fishUserBO.getUserId(),gold,arti,fishUserBO.getArtillerys());
        return gold;
    }

}
